package services

import (
	"regexp"
	"strings"
)

// 关键词：Overtaken、Abandoned、Overtaken.、Overtake
// 因前段查询是如果出现空格，根据3wc标准会转换为+号，查询失败，故用下划线代替空格
var tradeMap = map[string]string{
	"purchased by":      "购买",
	"sold by":           "出售",
	"Before purchasing": "购买前",
	"Before selling":    "出售前",
	"After tradeable":   "交易后",
}

type TradeRule struct {
}

/**
ActionTime string `gorm:"column:action_time;type:datetime(3);not null"`
UserId     uint64 `gorm:"column:user_id;type:bigint(20) unsigned;not null"`
UserName   string `gorm:"column:user_name;type:varchar(255);not null"`
StreamId   string `gorm:"column:stream_id;type:varchar(255);not null"`
TradeType  string `gorm:"column:trade_type;type:varchar(255);not null"`
ItemTxt    string `gorm:"column:item_txt;type:varchar(255);not null"`
Price      int    `gorm:"column:price;type:int(11);not null"`
Cash       int    `gorm:"column:cash;type:int(11);not null" comment:"现金"`
Merchant   string `gorm:"column:merchant;type:varchar(255);not null"`
OnlineNum  string `gorm:"column:online_num;type:varchar(255);not null"`
Balance    string `gorm:"column:balance;type:varchar(255);not null" comment:"账户余额"`
Gold       string `gorm:"column:gold;type:varchar(255);not null" comment:"黄金"`
*/
// 前19个字符,日期时间
func (r *TradeRule) GetActionTime(str string) string {
	return str[:19]
}

func (r *TradeRule) HandleFields(str string) (string, string, string, string, string, string, string, string, string, string, string, string) {
	//定义数据
	actionTime := ""
	userName := ""
	streamId := ""
	tradeType := ""
	trade_step := ""
	itemTxt := ""
	price := ""
	cash := ""
	merchant := ""
	balance := ""
	gold := ""
	online_num := ""

	if strings.Contains(str, "purchased by") {
		actionTime, userName, streamId, tradeType, trade_step, itemTxt, price, cash, merchant, balance, gold, online_num =
			r.purchasedHandle(str)
	}
	if strings.Contains(str, "sold by") {
		actionTime, userName, streamId, tradeType, trade_step, itemTxt, price, cash, merchant, balance, gold, online_num =

			r.soldHandle(str)
	}
	if strings.Contains(str, "Before purchasing") {
		actionTime, userName, streamId, tradeType, trade_step, itemTxt, price, cash, merchant, balance, gold, online_num =

			r.BeforePurchasingHandle(str)
	}
	if strings.Contains(str, "Before selling") {
		actionTime, userName, streamId, tradeType, trade_step, itemTxt, price, cash, merchant, balance, gold, online_num =

			r.BeforeSellingHandle(str)
	}
	if strings.Contains(str, "After tradeable") {
		actionTime, userName, streamId, tradeType, trade_step, itemTxt, price, cash, merchant, balance, gold, online_num =

			r.AfterTradeableHandle(str)
	}
	return actionTime, userName, streamId, tradeType, trade_step, itemTxt, price, cash, merchant, balance, gold, online_num
}

// 购买
func (r *TradeRule) purchasedHandle(str string) (string, string, string, string, string, string, string, string, string, string, string, string) {
	actionTime := r.GetActionTime(str)
	userName := ""
	streamId := "purchase"
	tradeType := "purchased_by"
	trade_step := ""
	itemTxt := ""
	price := ""
	cash := ""
	merchant := ""
	balance := ""
	gold := ""
	online_num := ""
	//Tradeable (Motorcycle_Boots_01 (x1)) purchased by 阿玉(76561199387552928) for 800 money from trader B_4_Trader, old amount in store was -1, new amount is -1, and effective users online: 22
	reg := regexp.MustCompile(`Tradeable \((.*)\) purchased by (.*)\((.*)\) for (.*) money from trader (.*).*and effective users online: (.*)`)
	if reg.MatchString(str) {
		match := reg.FindStringSubmatch(str)
		itemTxt = match[1]
		userName = match[2]
		streamId = match[3]
		price = match[4]
		merchant = match[5]
		online_num = match[6]
	}
	return actionTime, userName, streamId, tradeType, trade_step, itemTxt, price, cash, merchant, balance, gold, online_num
}

// 出售
func (r *TradeRule) soldHandle(str string) (string, string, string, string, string, string, string, string, string, string, string, string) {
	actionTime := r.GetActionTime(str)
	userName := ""
	streamId := ""
	tradeType := "sold"
	trade_step := "sold by"

	itemTxt := ""
	price := ""
	cash := ""
	merchant := ""
	balance := ""
	gold := ""
	online_num := ""
	//Tradeable (Weapon_VSS_VZ (x3)) sold by 321360378(76561199058823336) for 3114 (1308 + 1806 worth of contained items) to trader B_4_Armory, old amount in store is -1, new amount is -1, and effective users online: 22
	reg := regexp.MustCompile(`Tradeable \((.*)\) sold by (.*)\((.*)\) for (.*) \(.*\) to trader (.*), old amount in store is -1, new amount is -1, and effective users online: (.*)`)
	if reg.MatchString(str) {
		match := reg.FindStringSubmatch(str)
		itemTxt = match[1]
		userName = match[2]
		streamId = match[3]
		price = match[4]
		merchant = match[5]
		online_num = match[6]
	}
	return actionTime, userName, streamId, tradeType, trade_step, itemTxt, price, cash, merchant, balance, gold, online_num
}

// 购买前：Before purchasing
func (r *TradeRule) BeforePurchasingHandle(str string) (string, string, string, string, string, string, string, string, string, string, string, string) {
	actionTime := r.GetActionTime(str)
	userName := ""
	streamId := ""
	tradeType := "purchase"
	trade_step := "Before_purchasing"
	itemTxt := ""
	price := ""
	cash := ""
	merchant := ""
	balance := ""
	gold := ""
	online_num := ""
	//Before purchasing tradeales from trader B_4_Mechanic, player 野原广志(76561199404724549) had 0 cash, 1090640 account balance and 23 gold and trader had 100000 funds.
	reg := regexp.MustCompile(`Before purchasing tradeales from trader (.*), player (.*)\((.*)\) had (.*) cash, (.*) account balance and (.*) gold and trader had (.*) funds.`)
	if reg.MatchString(str) {
		match := reg.FindStringSubmatch(str)
		merchant = match[1]
		userName = match[2]
		streamId = match[3]
		cash = match[4]
		balance = match[5]
		gold = match[6]
		online_num = match[7]
	}
	return actionTime, userName, streamId, tradeType, trade_step, itemTxt, price, cash, merchant, balance, gold, online_num
}

// 出售前：Before selling
func (r *TradeRule) BeforeSellingHandle(str string) (string, string, string, string, string, string, string, string, string, string, string, string) {
	actionTime := r.GetActionTime(str)
	userName := ""
	streamId := ""
	tradeType := "sold"
	trade_step := "Before_selling"
	itemTxt := ""
	price := ""
	cash := ""
	merchant := ""
	balance := ""
	gold := ""
	online_num := ""
	//Before selling tradeables to trader B_4_Armory, player aptzydfo(76561199194698972) had 0 cash, 56625 account balance and 0 gold and trader had 100000 funds.
	reg := regexp.MustCompile(`Before selling tradeables to trader (.*), player (.*)\((.*)\) had (.*) cash, (.*) account balance and (.*) gold and trader had (.*) funds.`)
	if reg.MatchString(str) {
		match := reg.FindStringSubmatch(str)
		merchant = match[1]
		userName = match[2]
		streamId = match[3]
		cash = match[4]
		balance = match[5]
		gold = match[6]
		online_num = match[7]
	}
	return actionTime, userName, streamId, tradeType, trade_step, itemTxt, price, cash, merchant, balance, gold, online_num
}

// 交易后：After tradeable
func (r *TradeRule) AfterTradeableHandle(str string) (string, string, string, string, string, string, string, string, string, string, string, string) {
	actionTime := r.GetActionTime(str)
	userName := ""
	streamId := ""
	tradeType := "tradeable"
	trade_step := "After_tradeable"
	itemTxt := ""
	price := ""
	cash := ""
	merchant := ""
	balance := ""
	gold := ""
	online_num := ""
	//After tradeable sale to trader B_4_Armory, player aptzydfo(76561199194698972) has 83 cash, 56625 account balance and 0 gold and trader has 100000 funds.
	reg := regexp.MustCompile(`After tradeable sale to trader (.*), player (.*)\((.*)\) has (.*) cash, (.*) account balance and (.*) gold and trader has (.*) funds.`)
	if reg.MatchString(str) {
		match := reg.FindStringSubmatch(str)
		merchant = match[1]
		userName = match[2]
		streamId = match[3]
		cash = match[4]
		balance = match[5]
		gold = match[6]
		online_num = match[7]
	}
	return actionTime, userName, streamId, tradeType, trade_step, itemTxt, price, cash, merchant, balance, gold, online_num
}
